﻿Public Class Writer

    Public Sub writeHtmlTest()

        Dim meinBuch As HtmlWriter = New HtmlWriter("testbuch")

        meinBuch.writeTitle("das ist ein TestBuch")

        meinBuch.writeTextSpan("erste Seite")
        meinBuch.writeTextSpan("erste Seite zweite zeile")
        meinBuch.nextPage()
        meinBuch.writeTitle("das ist ein TestBuch")
        meinBuch.writeText("zweite Seite")
        meinBuch.nextPage()
        meinBuch.writeTitle("das ist ein TestBuchas")
        meinBuch.writeText("dritte Seite")
        meinBuch.nextPage()
        meinBuch.writeTitle("das ist ein TestBuchasd")
        meinBuch.writeText("dritte Seite und so")
        meinBuch.finishBook()
        System.Windows.Forms.MessageBox.Show("Book created !")

    End Sub

    Public Sub writeHtml(title As String, doc As Object)
        Dim newBook As HtmlWriter = New HtmlWriter(title)

        ' max 12 lines
        ' max 49 chars per line
        Dim linesWritten As Integer = 0


        ' title of book
        ' newBook.writeTitle(title)

        ' placeholder document
        ' Dim doc As MyDocument = New MyDocument

        ' iterate paragraphs of document
        For Each paragraph As MyParagraph In doc.paragraphs

            If linesWritten = 22 Then
                newBook.nextPage()
                linesWritten = 0
            End If

            ' create new string for words of paragraph
            Dim newWordString As String = ""

            ' empty paragraph is empty line -> write
            If paragraph.empty Then
                ' write empty line
                newBook.writeLineBrake()
                linesWritten += 1
                ' if paragraph empty go to next paragraph
                Continue For
            End If

            'If paragraph Is Nothing Then
            'newBook.writeBlankLine()
            'Continue For
            'End If

            ' iterate sentences of paragraph
            For Each sentence As MySentence In paragraph.sentences

                ' iterate words of sentence
                For i = 0 To sentence.words.Length - 1
                    ' if is nothing -> no word but special character
                    If sentence.words(i) Is Nothing Then
                        ' special char handling
                        If sentence.specialChars(i) = "<TAB>" Then
                            ' special char is tab, write string to file, use insert tab function and reset word
                            newBook.writeTextSpan(newWordString)
                            newBook.writeTab()
                            ' ToDo: check if reseting string this way works or is memory leak - delete object necessary or way done is sufficient
                            newWordString = ""
                        Else
                            'unknown special char
                        End If
                    Else
                        ' regular word string
                        If newWordString Is "" Then
                            newWordString += sentence.words(i)
                        Else
                            ' too long for one line
                            If (newWordString.Length + sentence.words(i).Length) > 60 Then
                                newBook.writeTextSpan(newWordString)
                                newBook.writeLineBrake()
                                linesWritten += 1
                                newWordString = sentence.words(i)
                            Else
                                ' newWordString += " "
                                newWordString += sentence.words(i)
                            End If
                        End If
                    End If
                Next

            Next

            ' paragraph done -> write string and zeilenumbruch
            newBook.writeTextSpan(newWordString)
            linesWritten += 1
            newBook.writeLineBrake()

        Next

        ' ToDo where and how is new page saved, appropriate place to insert new page needs to be determined

        'finish book
        newBook.finishBook()


    End Sub

    Public Sub writeString()
        Dim newString As String = "blabla"
        Dim objWriter As New System.IO.StreamWriter("D:\test.html")
        objWriter.Write(newString)
        objWriter.Close()
        objWriter.Dispose()
        System.Windows.Forms.MessageBox.Show("Stuff written !")
    End Sub

End Class